package cn.gok.oa.system.controller;
import cn.gok.oa.log.LogAnnotation;
import cn.gok.oa.log.entity.Log;
import cn.gok.oa.system.dto.DeptDto;
import cn.gok.oa.system.entity.TbDept;
import cn.gok.oa.system.entity.TbUser;
import cn.gok.oa.system.mapper.TbDeptMapper;
import cn.gok.oa.system.mapper.TbUserMapper;
import cn.gok.oa.util.ResultData;
import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gok
 * @since 2022-08-04
 */

@RestController
@RequestMapping("/system/tb-dept")
public class TbDeptController {
    //添加模块

    @Autowired
    TbDeptMapper tbDeptMapper;

    @Autowired
    TbUserMapper tbUserMapper;

    //部门添加模块
    @LogAnnotation("部门表添加操作")
    @RequestMapping("/add")
    @ResponseBody
    public ResultData add(TbDept tbDept,HttpServletRequest httpServletRequest){
        String token = httpServletRequest.getHeader("token");
        int createId = Integer.parseInt(JWT.decode(token).getAudience().get(0));
        tbDept.setCreateId(createId);
        int temp = tbDeptMapper.insert(tbDept);
        if (temp > 0){
            return ResultData.success();
        }
        return ResultData.fail();
    }
    //部门删除模块
    @LogAnnotation("部门表删除操作")
    @RequestMapping("/delete")
    @ResponseBody
    public ResultData delete(Integer id,HttpServletRequest httpServletRequest){
        TbDept tbDept = new TbDept();
        String token = httpServletRequest.getHeader("token");
        int modifyId = Integer.parseInt(JWT.decode(token).getAudience().get(0));
        tbDept.setModifyId(modifyId);
        int temp=0;
        HashMap<String,Object> map=new HashMap<>();
        map.put("dept_id",id);
        List<TbUser> users = tbUserMapper.selectByMap(map);
        if (users.size()==0 ) {
          // tbDeptMapper.updateById(tbDept);
            temp= tbDeptMapper.deleteById(id);
        }
        if (temp > 0){
            return ResultData.success();
        }
        return ResultData.fail();

    }
    //修改模块
    @LogAnnotation("部门表更新操作")
    @RequestMapping("/update")
    @ResponseBody
    public ResultData update(TbDept tbDept, HttpServletRequest httpServletRequest){

        String token = httpServletRequest.getHeader("token");
        int modifyId = Integer.parseInt(JWT.decode(token).getAudience().get(0));
        tbDept.setModifyId(modifyId);
       //tbUserMapper.updateUser();
        Integer temp = tbDeptMapper.updateById(tbDept);
        if (temp > 0){
            return ResultData.success();
        }
        return ResultData.fail();
    }
    //分页查询
    @LogAnnotation("部门表查询操作")
    @RequestMapping("/select")
    @ResponseBody
    public ResultData select(DeptDto deptDto){
        Page<TbDept> page = new Page<>(deptDto.getPage(),deptDto.getLimit());

        //组装查询条件
        QueryWrapper<TbDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(deptDto.getName()),"name",deptDto.getName());
        queryWrapper.eq(deptDto.getDeptStatus()!=null ,"deptStatus",deptDto.getDeptStatus());

        //调用mybatis plus 查询分页
        tbDeptMapper.selectPage(page,queryWrapper);
        return  ResultData.buildData(page);

    }
}


